// Copyright 2007-2010 Comdiv (F. Sadykov) - http://code.google.com/u/fagim.sadykov/
// Supported by Media Technology LTD 
//  
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//  
//      http://www.apache.org/licenses/LICENSE-2.0
//  
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// 
// MODIFICATIONS HAVE BEEN MADE TO THIS FILE
using System;
using System.Collections.Generic;
using Comdiv.Model;
using NHibernate.Criterion;

namespace Comdiv.Security.Acl{
    public class DefaultAclDatabaseRuleProvider : IAclDatabaseRuleProvider{
        public DefaultAclDatabaseRuleProvider(){
            Idx = 1000;
        }
        public int Idx
        {
            get; set;
        }

        public Exception ErrorState { get; set; }

        public IEnumerable<IAclRule> GetRules(){
            checkSupport();
            if (ErrorState != null) yield break;
            foreach (var rule in Persister.All<IAclRule>()){
                rule.Evidence = "database";
                yield return rule;
            }
        }

        public void Add(IAclRule rule){
            checkSupport();
            if(ErrorState!=null) return;
            AclRuleEntity ent = (rule is AclRuleEntity)
                                    ? (AclRuleEntity) rule
                                    : new AclRuleEntity
                                          {
                                              Active = rule.Active,
                                              Name =
                                                  rule.PrincipalMask + rule.TokenMask + rule.RuleType +
                                                  Environment.TickCount,
                                              EndDate = rule.EndDate,
                                              Permissions = rule.Permissions,
                                              TokenMask = rule.TokenMask,
                                              PrincipalMask = rule.PrincipalMask,
                                              RuleType = rule.RuleType,
                                              System = rule.System

                                          };
    
                    Persister.Save(ent);
                
            
        }

        public void Remove(IAclRule rule){
            checkSupport();
            if (ErrorState != null) return;
            if (rule is AclRuleEntity)
            {
                //using (var s = new SessionWrapperHandler(true)){
                    Persister.Delete(rule);
                  //  s.VoteCommit();
                //}
            }
            else
            {
                throw new NotSupportedException("cannot remove of type " + rule.GetType().FullName + " from database");
            }
        }

        public void Clear(){
            throw new NotSupportedException("database cleaning is very harmful operation");
        }

        public void checkSupport(){
            try{
                Persister.First<IAclRule>(Restrictions.Ge("Id", 0));
            }
            catch(Exception ex){
                ErrorState = ex;
            }
        }
    }
}